<?php
declare (strict_types=1);

namespace app\admin\service\system\notify;

use app\admin\logic\system\Config as SystemConfig;
use app\admin\logic\system\notify\NotifyNode as NotifyNodeLogic;
use app\common\service\BaseService;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageForm;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;

class NotifyNode extends BaseService
{

    /**
     * @Inject()
     *
     * @var NotifyNodeLogic
     */
    protected $notifyNodeLogic;

    /**
     * 列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-01
     *
     * @return   [type]                         [description]
     */
    public function index()
    {
        $map = [];
        Request::has('title', 'get', true) && $map[] = ['title', 'like', '%' . Request::get('title/s') . '%'];
        $data = $this->notifyNodeLogic->getList($map);
        // 定义字段
        $keyList = [
            'key'              => ListItem::make()->title('通知节点标识'),
            'title'            => ListItem::make()->title('通知名称'),
            'data_update_time' => ListItem::make()->title('最后更新时间'),
        ];

        $searchKey = [
            'title' => FormItem::make('input')->title(false)->placeholder('名称')->maxlength(30),
        ];

        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey   = $keyList;
        $list->datas     = $data;
        $list->searchKey = $searchKey;

        return $list->send();
    }

    /**
     * 编辑
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-01
     *
     * @param int $id [description]
     * @return   [type]                             [description]
     */
    public function config(int $id)
    {
        if (Request::isPut()) {
            $data = Request::put();
            $res  = $this->notifyNodeLogic->saveNode($id, $data);

            return $this->parseToData($res->toArray(), 1, '配置成功');
        }

        $data = $this->notifyNodeLogic->getInfoByIdOrFail($id);

        $open        = [['title' => '系统通知', 'value' => 'system_notify']];
        $has_sms     = $data->getAttr('has_sms');
        $smsHelpInfo = '1. 填写第三方对应的模板ID<br />2. 当开启发送短信时必填';

        if ($has_sms) {
            $open[] = ['title' => '短信', 'value' => 'sms'];
            if (!request()->is_platform) {
                // 当前是机构且开启了短信使用平台配置
                $config      = SystemConfig::get('system.package.sms.sync', ['use_platform' => 0], request()->mhm_id, true);
                $usePlatform = isset($config['use_platform']) ? (bool)$config['use_platform'] : false;
                $smsHelpInfo = $usePlatform ? $smsHelpInfo . '<br /><font style="color:red;">3. 检测到当前短信配置启用了平台配置，短信模板配置不会生效，将使用平台配置</font>' : $smsHelpInfo;
            }
        }

        if (is_null($data->getAttr('open_info'))) {
            $data->open_info = [];
        }

        $help = $data->getAttr('help') ?: '';

        // 设置表单
        $form    = app(PageForm::class);
        $keyList = [
            'title'             => FormItem::make()->title('通知名称')->required()->disabled(),
            'open_info'         => FormItem::make('checkbox')->title('开启通知')->options($open),
            'content'           => FormItem::make('textarea')->title('通知内容')->required()->rows(6)->help($help),
            'sms_template_code' => FormItem::make()->title('短信通知模板')->help($smsHelpInfo)->visible($has_sms),

        ];
        // 表单字段
        $form->pageKey = $keyList;

        $form->datas = $data;

        $form->saveMethod  = 'PUT';
        $form->savePostUrl = '/admin/system/notify/node/' . $id . '/config';

        return $form->send();
    }

}
